package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.Assets;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.THouseAssets;
import com.ruoyi.system.domain.TLandAssets;
import com.ruoyi.system.domain.TLandHouseAssets;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * assetsController
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@Controller
@RequestMapping("/landhouse/assets")
public class TLandHouseAssetsController extends BaseController
{
    private String prefix = "assets";

    @Autowired
    private ITLandHouseAssetsService tLandHouseAssetsService;
    @Autowired
    private ITLandAssetsService itLandAssetsService;
    @Autowired
    private ITHouseAssetsService itHouseAssetsService;
    @Autowired
    private IAssetsService assetsService;
    @Autowired
    private ISysConfigService configService;

    @RequiresPermissions("landhouse:assets:view")
    @GetMapping()
    public String assets(ModelMap mmap)
    {
        mmap.put("key","landhouse");
        return prefix + "/assets";
    }

    /**
     * 查询assets列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TLandHouseAssets tLandHouseAssets,String deptId)
    {
        Assets assets = new Assets();
        List<TLandHouseAssets> list = new ArrayList<>();
        assets.setParentId(Long.valueOf(deptId));
        List<Assets> assetsList = assetsService.selectDeptList(assets);
        startPage();
        if (assetsList != null && assetsList.size()>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0;i<assetsList.size(); i++){
                Assets assets1 = assetsList.get(i);
                Long id = assets1.getDeptId();
                sb.append(id);
                if ( i != assetsList.size()-1) {
                    sb.append(",");
                }
            }
            list = tLandHouseAssetsService.selectTLandAssetsListByIds(sb.toString());
            if (list.size() == 0 ){
                startPage();
                list = tLandHouseAssetsService.selectTHouseAssetsListByIds(sb.toString());
            }
        }else {
            tLandHouseAssets.setType(deptId);
            list = tLandHouseAssetsService.selectTLandAssetsList(tLandHouseAssets);
            if (list.size() == 0 ){
                PageUtils.startPage();
                list = tLandHouseAssetsService.selectTHouseAssetsList(tLandHouseAssets);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出assets列表
     */
    @RequiresPermissions("landhouse:assets:export")
    @Log(title = "assets", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TLandHouseAssets tLandHouseAssets)
    {
        List<TLandHouseAssets> list = tLandHouseAssetsService.selectTLandHouseAssetsList(tLandHouseAssets);
        ExcelUtil<TLandHouseAssets> util = new ExcelUtil<TLandHouseAssets>(TLandHouseAssets.class);
        return util.exportExcel(list, "assets数据");
    }

    /**
     * 新增assets
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/addbyId/{deptId}")
    public String addbyId(@PathVariable("deptId")String deptId,ModelMap mmap)
    {
        Assets assets = assetsService.selectDeptById(Long.valueOf(deptId));
        String urlAdd = assets.getUrlAdd();
        TLandHouseAssets landHouseAssets = new TLandHouseAssets();
        landHouseAssets.setType(deptId);
        mmap.put("landHouseAssets",landHouseAssets);
        return urlAdd;
    }

    /**
     * 新增保存assets
     */
    @RequiresPermissions("landhouse:assets:add")
    @Log(title = "assets", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TLandHouseAssets tLandHouseAssets)
    {
        return toAjax(tLandHouseAssetsService.insertTLandHouseAssets(tLandHouseAssets));
    }

    /**
     * 修改assets
     */
    @RequiresPermissions("landhouse:assets:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TLandHouseAssets tLandHouseAssets = tLandHouseAssetsService.selectTLandHouseAssetsById(id);
        mmap.put("tLandHouseAssets", tLandHouseAssets);
        return prefix + "/edit";
    }

    @RequiresPermissions("landhouse:assets:edit")
    @GetMapping("/edit/{id}/{deptId}")
    public String edits(@PathVariable("id") Long id,@PathVariable("deptId")String deptId, ModelMap mmap)
    {
        Assets assets = assetsService.selectDeptById(Long.valueOf(deptId));
        String urlEdit = assets.getUrlEdit();
        TLandHouseAssets tLandHouseAssets = tLandHouseAssetsService.selectTLandHouseAssetsById(id);
        mmap.put("tLandHouseAssets", tLandHouseAssets);
        return urlEdit;
    }

    /**
     * 修改保存assets
     */
    @RequiresPermissions("landhouse:assets:edit")
    @Log(title = "assets", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TLandHouseAssets tLandHouseAssets)
    {
        return toAjax(tLandHouseAssetsService.updateTLandHouseAssets(tLandHouseAssets));
    }

    /**
     * 删除assets
     */
    @RequiresPermissions("landhouse:assets:remove")
    @Log(title = "assets", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] split = ids.split(",");
        int i = 0;
        for (String s : split){
            TLandHouseAssets tLandHouseAssets = tLandHouseAssetsService.selectTLandHouseAssetsById(Long.valueOf(s));
            if (tLandHouseAssets != null ){
                if (tLandHouseAssets.getCaseNo() != null){
                    TLandAssets tLandAssets = new TLandAssets();
                    tLandAssets.setDelFlag(1);
                    tLandAssets.setId(s);
                    int i1 = itLandAssetsService.updateTLandAssets(tLandAssets);
                    i=i+i1;
                }else {
                    THouseAssets tHouseAssets = new THouseAssets();
                    tHouseAssets.setId(s);
                    tHouseAssets.setDelFlag(1);
                    int i1 = itHouseAssetsService.updateTHouseAssets(tHouseAssets);
                    i=i+i1;
                }
            }
        }
        return toAjax(i);
    }

}
